﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Edu.Web.Data.DV;
using Edu.Web.Data.Entity;

namespace Edu.Web.Data.DAO.Impl
{
    public partial class ClazzDAO
    {
        public IQueryable<ClazzListItemDV> LoadDataByTeacherUid(long uid)
        {
            var query = from clazz in Db.Set<ClazzEntity>()
                        join clazz2 in Db.Set<ClazzTeacherEntity>() on clazz.Id equals clazz2.ClazzId into newClazz
                        from clazzItem in newClazz
                        join teacher in Db.Set<TeacherEntity>() on clazzItem.TeacherUserId equals teacher.UserId
                        where teacher.UserId == uid
                        select new ClazzListItemDV
                        {
                            ClazzId = clazz.Id,
                            ClazzName = clazz.ClazzName,
                        };
            return query;
        }

        public IQueryable<ClassStudentDV> LoadStudentListByClazzId(long clazzId)
        {
            return from clazz in Db.Set<ClazzEntity>()
                   join clazzStu in Db.Set<ClazzStuEntity>() on clazz.Id equals clazzStu.ClazzId into clazzStuSet
                   from clazzStuItem in clazzStuSet
                   join stu in Db.Set<StudentEntity>() on clazzStuItem.StuUserId equals stu.UserId into newStuSet
                   from newStuSetItem in newStuSet
                   join user in Db.Set<UserEntity>() on newStuSetItem.UserId equals user.Id
                   where clazz.Id == clazzId
                   select new ClassStudentDV
                   {
                       Gender = user.Gender,
                       UserId = user.Id,
                       StuNo = newStuSetItem.StuNo,
                       RealName = user.RealName
                   };
        }

        public IQueryable<ClazzListItemDV> LoadDataByStuUid(long uid)
        {
            var query = from clazz in Db.Set<ClazzEntity>()
                        join clazz2 in Db.Set<ClazzStuEntity>() on clazz.Id equals clazz2.ClazzId into newClazz
                        from clazzItem in newClazz
                        join student in Db.Set<StudentEntity>() on clazzItem.StuUserId equals student.UserId
                        where student.UserId == uid
                        select new ClazzListItemDV
                        {
                            ClazzId = clazz.Id,
                            ClazzName = clazz.ClazzName,
                        };
            return query;
        }
    }
}